package cleancode.util;

import java.util.Collection;
import java.util.TreeSet;

import cleancode.model.NameBasedModel;
import cleancode.model.NameOrientedModelSet;

/**
 * String operation utils.
 */
public class StringUtils
{

    public static String combineModelNamesWithComma( NameOrientedModelSet<? extends NameBasedModel> set )
    {
        TreeSet<String> nameSet = new TreeSet<>();
        for( NameBasedModel model : set )
        {
            nameSet.add( model.getName() );
        }
        return combineCollection( nameSet, ',' );
    }

    public static String combineCollectionsWithSemi( Collection<?> collection )
    {
        return combineCollection( collection, ';' );
    }

    public static String combineCollectionsWithRow( Collection<?> collection )
    {
        return combineCollection( collection, '\n' );
    }

    public static String combineCollection( Collection<?> collection, char connector )
    {
        StringBuilder builder = new StringBuilder();
        for( Object model : collection )
        {
            builder.append( model.toString() + connector );
        }
        return builder.length() == 0 ? "" : builder.substring( 0, builder.length() - 1 );
    }

    public static String trimComma( String input )
    {
        String ret = input;
        while( input.startsWith( "," ) )
        {
            ret = ret.substring( 1 );
        }
        while( input.endsWith( "," ) )
        {
            ret = ret.substring( 0, ret.length() - 1 );
        }
        return ret;
    }

    public static String rightPadding( String input )
    {
        return String.format( "%-60s", input );
    }
}
